<template>
  <div class="activity-edit">
    <el-header class="header">
      <div class="header-content">
        <div class="logo">
          <h2>🏃‍♂️ 健身活动管理系统</h2>
        </div>
        <div class="nav">
          <el-button @click="$router.push('/admin/dashboard')" type="primary">仪表板</el-button>
          <el-button @click="$router.push('/admin/activities')">活动管理</el-button>
          <el-button @click="$router.push('/admin/bookings')">预约管理</el-button>
          <el-button @click="logout" type="danger">退出登录</el-button>
        </div>
      </div>
    </el-header>
    <el-main class="main-content">
      <el-card class="edit-card">
        <template #header>
          <div class="card-header">
            <span>✏️ 编辑活动</span>
          </div>
        </template>

        <el-form v-if="activity" :model="activityForm" :rules="activityRules" ref="activityFormRef" label-width="120px">
          <el-form-item label="活动名称" prop="title">
            <el-input v-model="activityForm.title" placeholder="请输入活动名称" />
          </el-form-item>
          
          <el-form-item label="活动描述" prop="description">
            <el-input
              v-model="activityForm.description"
              type="textarea"
              :rows="3"
              placeholder="请输入活动描述"
            />
          </el-form-item>
          
          <el-form-item label="活动地点" prop="location">
            <el-input v-model="activityForm.location" placeholder="请输入活动地点" />
          </el-form-item>
          
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              v-model="activityForm.startTime"
              type="datetime"
              placeholder="选择开始时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
              v-model="activityForm.endTime"
              type="datetime"
              placeholder="选择结束时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          
          <el-form-item label="最大参与人数" prop="maxParticipants">
            <el-input-number
              v-model="activityForm.maxParticipants"
              :min="1"
              :max="100"
              placeholder="请输入最大参与人数"
            />
          </el-form-item>
          
          <el-form-item label="当前参与人数" prop="currentParticipants">
            <el-input-number
              v-model="activityForm.currentParticipants"
              :min="0"
              :max="activityForm.maxParticipants"
              placeholder="当前参与人数"
            />
          </el-form-item>
          
          <el-form-item label="活动价格" prop="price">
            <el-input-number
              v-model="activityForm.price"
              :min="0"
              :precision="2"
              placeholder="请输入活动价格"
            />
          </el-form-item>
          
          <el-form-item label="活动状态" prop="status">
            <el-radio-group v-model="activityForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">已取消</el-radio>
            </el-radio-group>
          </el-form-item>
          
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="activityForm.remark"
              type="textarea"
              :rows="2"
              placeholder="请输入备注信息（可选）"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="updateActivity" :loading="loading">
              {{ loading ? '更新中...' : '更新活动' }}
            </el-button>
            <el-button @click="$router.push('/admin/activities')">取消</el-button>
          </el-form-item>
        </el-form>
        
        <el-empty v-else description="活动不存在" />
      </el-card>
    </el-main>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { activityApi } from '@/api'

const router = useRouter()
const route = useRoute()

const activity = ref(null)
const loading = ref(false)
const activityFormRef = ref()

const activityForm = reactive({
  id: '',
  title: '',
  description: '',
  location: '',
  startTime: '',
  endTime: '',
  maxParticipants: 20,
  currentParticipants: 0,
  price: 0,
  status: 1,
  remark: ''
})

const activityRules = {
  title: [
    { required: true, message: '请输入活动名称', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入活动描述', trigger: 'blur' }
  ],
  location: [
    { required: true, message: '请输入活动地点', trigger: 'blur' }
  ],
  startTime: [
    { required: true, message: '请选择开始时间', trigger: 'change' }
  ],
  endTime: [
    { required: true, message: '请选择结束时间', trigger: 'change' }
  ],
  maxParticipants: [
    { required: true, message: '请输入最大参与人数', trigger: 'blur' }
  ],
  price: [
    { required: true, message: '请输入活动价格', trigger: 'blur' }
  ]
}

const loadActivity = async () => {
  try {
    const id = route.params.id
    const res = await activityApi.getActivityDetail(id)
    activity.value = res.data.activity
    
    if (activity.value) {
      Object.assign(activityForm, activity.value)
    }
  } catch (error) {
    console.error('加载活动信息失败:', error)
    ElMessage.error('加载活动信息失败')
  }
}

const updateActivity = async () => {
  try {
    await activityFormRef.value.validate()
    loading.value = true
    
    await activityApi.updateActivity(activityForm)
    ElMessage.success('活动更新成功')
    router.push('/admin/activities')
  } catch (error) {
    console.error('更新活动失败:', error)
  } finally {
    loading.value = false
  }
}

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  ElMessage.success('退出登录成功')
  router.push('/login')
}

onMounted(() => {
  loadActivity()
})
</script>

<style scoped>
.activity-edit {
  min-height: 100vh;
  background: #f5f7fa;
}

.header {
  background: white;
  border-bottom: 1px solid #e0e0e0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.logo h2 {
  margin: 0;
  color: #333;
}

.nav .el-button {
  margin-left: 10px;
}

.main-content {
  padding: 20px;
}

.edit-card {
  max-width: 800px;
  margin: 0 auto;
  border-radius: 15px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 